//
//  ViewController.m
//  20-XMLSAXAndDOM
//
//  Created by dllo on 16/8/10.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ViewController.h"
#import "Student.h"
#import "GDataXMLNode.h"
#import "XMLReader.h"

@interface ViewController ()<NSXMLParserDelegate>
@property (nonatomic,strong) NSMutableArray *stuArray;
@property (nonatomic,copy) NSString *currentElementName;
@property (nonatomic,strong) NSMutableArray *domStuArray;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
//    将xml结构，转换成字典
    NSString *xmlPath = [[NSBundle mainBundle] pathForResource:@"stu_xml" ofType:@"xml"];
    
    NSData *xmlData = [NSData dataWithContentsOfFile:xmlPath];
    NSError *error = nil;
    NSDictionary *xmlDic = [XMLReader dictionaryForXMLData:xmlData error:&error];
//    NSLog(@"%@",xmlDic);
    
}
//xml分为两种解析方式
//第一种SAX逐行解析 使用系统的解析器NSXMLParser
- (IBAction)saxParse:(id)sender {
   NSString *xmlPath = [[NSBundle mainBundle] pathForResource:@"stu_xml" ofType:@"xml"];
    
    NSData *xmlData = [NSData dataWithContentsOfFile:xmlPath];
    NSXMLParser *xmParser = [[NSXMLParser alloc] initWithData:xmlData];
//    NSLog(@"%@",xmlPath);
//    NSLog(@"%@",xmParser);
    xmParser.delegate = self;
    [xmParser parse];
    
    
}
//解析器开始
- (void)parserDidStartDocument:(NSXMLParser *)parser
{
    _stuArray = [NSMutableArray array];
}
//遇到节点头，方法的回调,记录当前节点，方便后续赋值
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary<NSString *,NSString *> *)attributeDict
{
    _currentElementName = elementName;
    if ([elementName isEqualToString:@"student"]) {
        Student *stu = [Student new];
        [_stuArray addObject:stu];
    }
//    NSLog(@"%@",_stuArray);
// NSLog(@"节点的头%@",elementName);
}
//节点的值，根据节点内容决定值 没有值对应的结果\n\t \n\t\t
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    //    string是 \n\t
//    第二种方法
    if (_currentElementName) {
        Student *stu = [_stuArray lastObject];
        [stu setValue:string forKey:_currentElementName];
    }
//    第一种方法
    if (![string hasSuffix:@" "] && ![string hasSuffix:@"\n"]) {
        //        NSLog(@"%@",string);
        Student *stu = [_stuArray lastObject];
//        NSLog(@"%@",stu);
        [stu setValue:string forKey:_currentElementName];
//        NSLog(@"%@",stu);
//        NSLog(@"节点的值%@-------节点值结束",string);
    }
    
}
//4遇到节点尾，方法的回调，判断当前节点的结束
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
    _currentElementName = nil;//第二种方法
// NSLog(@"/结点的尾%@",elementName);
    
    
//    NSLog(@"%@",);
}

//解析器结束
- (void)parserDidEndDocument:(NSXMLParser *)parser
{
//    NSLog(@"%@",_stuArray);
}

- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError
{
    NSLog(@"解析失败%@",parseError);
}

//第二种解析方式，DOM解析
//关于GDataXMLnODE的引入
//1引入.h和.m文件
//2引入系统动态库libxml2.tbd
//3根据GDataXMLnODE头文件的提示添加/usr/include/libxml2
//4如果是ARC环境，需要对该文件进行MRC编译，添加-fno-objc-arc
- (IBAction)domParse:(id)sender {
    _domStuArray = [NSMutableArray array];
    NSString *xmlPath = [[NSBundle mainBundle] pathForResource:@"stu_xml" ofType:@"xml"];
    NSData *xmlData = [NSData dataWithContentsOfFile:xmlPath];
    NSError *error = nil;
//    创建GDataXMLDocument对象
//    1根据xml的data数据生成一个树形结构的document
    GDataXMLDocument *document = [[GDataXMLDocument alloc] initWithData:xmlData options:0 error:&error];
    
    //    2根据获取document跟节点
    GDataXMLElement *rootElement = [document rootElement];
//    NSLog(@"%@",rootElement.name);
//   3 根据根节点获取子节点数组
    NSArray *rootChildren = [rootElement children];
//    4遍历子节点数组
    for (GDataXMLElement *childrenElement in rootChildren) {
//        NSLog(@"%@",childrenElement.name);
//        NSArray *arr = [childrenElement children];
//        5对应所有Student节点
        Student *stu = [Student new];
        [_domStuArray addObject:stu];
//        6遍历Student节点的所有子节点
        for (GDataXMLElement *grandSonElement in [childrenElement children]) {
//            NSLog(@"%@",grandSonElement.name);
//            7赋值
            [stu setValue:grandSonElement forKey:grandSonElement.name];
        }
    }
// NSLog(@"%@",_domStuArray);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
